using System;
using System.Configuration;
using System.Data;
using AspAlliance.SimpleCms;
using System.Data.SqlClient;
using System.Text;
using System.Xml;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace AspAlliance.SimpleCms.Test
{
    /// <summary>
    /// Summary description for DataHelperTest
    /// </summary>
    [TestClass]
    public class DataHelperTest
    {
        protected string _connectionString = ConfigurationManager.ConnectionStrings["CmsDbTest"].ConnectionString;
        private DataHelper _dataHelper;

        public DataHelperTest()
        {
        }

        [TestInitialize]
        public void MyTestInitialize()
        {
            _dataHelper = new DataHelper(_connectionString);
        }


        protected void ClearDatabase()
        {
            try
            {
                SqlHelper.ExecuteNonQuery(_connectionString, CommandType.Text, "DROP TABLE [cms_Page]");
            }
            catch (SqlException)
            {
                //HACK: Fail Silently
            }
        }

        [TestMethod]
        public void SetupTableTest()
        {
            ClearDatabase();
            _dataHelper.SetupTable();
            SqlHelper.ExecuteNonQuery(_connectionString, CommandType.Text, "UPDATE [cms_Page] SET [Title] = [Title]");
        }

        [TestMethod]
        public void VerifyDataBase()
        {
            var testConnection = new SqlConnection(_connectionString);
            testConnection.Open();
            Assert.AreEqual(ConnectionState.Open, testConnection.State, "The free-king DB does not exist.");
            testConnection.Close();
            Assert.AreEqual(ConnectionState.Closed, testConnection.State);
        }

        [TestMethod]
        public void SelectAllPagesTest()
        {
            DataTable allPages = _dataHelper.SelectAllPages();
            foreach (DataRow eachPage in allPages.Rows)
            {
                string pageName = null;
                pageName = eachPage["Url"].ToString();
                Assert.AreNotEqual(string.Empty, pageName);
                Assert.AreNotEqual(null, pageName);
            }
        }

        [TestMethod]
        public void SelectFolderPagesTest()
        {
            DataTable allPages = _dataHelper.SelectFolderPages("CMS");
            foreach (DataRow eachPage in allPages.Rows)
            {
                string pageName = null;
                pageName = eachPage["Url"].ToString();
                Assert.AreNotEqual(string.Empty, pageName);
                Assert.AreNotEqual(null, pageName);
            }
        }

        [TestMethod]
        public void SelectPageTest()
        {
            string Url = "~/myselecturl.cmsx";
            string Title = "Title";
            string Description = "Description";
            string Keywords = "Keywords";
            string ContentText = "ContentText";
            bool Enabled = true;

            SqlHelper.ExecuteNonQuery(_connectionString, CommandType.Text, "INSERT INTO [cms_Page] ([Url], [Title], [Description], [Keywords], [ContentText], [Enabled]) VALUES (@Url, @Title, @Description, @Keywords, @ContentText, @Enabled)",
                new SqlParameter[] { 
                    new SqlParameter("@Url",Url), 
                    new SqlParameter("@Title",Title), 
                    new SqlParameter("@Description",Description), 
                    new SqlParameter("@Keywords",Keywords), 
                    new SqlParameter("@ContentText",ContentText), 
                    new SqlParameter("@Enabled",Enabled) 
                });
            DataRow page = _dataHelper.SelectPage("~/myselecturl.cmsx");
            var dbPage = new CmsPage(page);
            Assert.AreEqual(Url, dbPage.Url);
            Assert.AreEqual(Title, dbPage.Title);
            Assert.AreEqual(Description, dbPage.Description);
            Assert.AreEqual(Keywords, dbPage.Keywords);
            Assert.AreEqual(ContentText, dbPage.ContentText);
            Assert.AreEqual(Enabled, dbPage.Enabled);
        }

        [TestMethod] 
        [Ignore]
        public void InsertPageTest()
        {
            //CmsPage myPage = new CmsPage(0, "~/myinserturl.cmsx", "Title", "Description", "Keywords", "ContentText", true);
            //DataHelper.InsertPage(_connectionString, myPage);
            //DataRow dbPage = SqlHelper.ExecuteDataset(_connectionString, 
            //    CommandType.Text, "SELECT * FROM [cms_Page] WHERE [Url] = '~/myinserturl.cmsx'").Tables[0].Rows[0];
            //Assert.AreEqual(myPage.Url, dbPage["Url"].ToString());
            //Assert.AreEqual(myPage.Title, dbPage["Title"].ToString());
            //Assert.AreEqual(myPage.Description, dbPage["Description"].ToString());
            //Assert.AreEqual(myPage.Keywords, dbPage["Keywords"].ToString());
            //Assert.AreEqual(myPage.ContentText, dbPage["ContentText"].ToString());
            //Assert.AreEqual(myPage.Enabled, (bool)dbPage["Enabled"]);
        }

        [TestMethod]
        public void TogglePageStatusTest()
        {
            DataRow myBeginningPage = SqlHelper.ExecuteDataset(_connectionString, 
                CommandType.Text, "SELECT * FROM [cms_Page] WHERE [Id] = 1").Tables[0].Rows[0];

            _dataHelper.TogglePageStatus("1");

            DataRow myEndingPage = SqlHelper.ExecuteDataset(_connectionString, 
                CommandType.Text, "SELECT * FROM [cms_Page] WHERE [Id] = 1").Tables[0].Rows[0];

            Assert.AreEqual(myBeginningPage["Url"].ToString(), myEndingPage["Url"].ToString());
            Assert.AreEqual(myBeginningPage["Title"].ToString(), myEndingPage["Title"].ToString());
            Assert.AreEqual(myBeginningPage["Description"].ToString(), myEndingPage["Description"].ToString());
            Assert.AreEqual(myBeginningPage["Keywords"].ToString(), myEndingPage["Keywords"].ToString());
            Assert.AreEqual(myBeginningPage["ContentText"].ToString(), myEndingPage["ContentText"].ToString());
            Assert.AreEqual(!(bool)myBeginningPage["Enabled"], (bool)myEndingPage["Enabled"]);
        }

        [TestMethod]
        public void UpdatePageTest()
        {
            DataRow pageData1 = SqlHelper.ExecuteDataset(_connectionString, 
                CommandType.Text, "SELECT * FROM [cms_Page] WHERE [Id] = 1").Tables[0].Rows[0];

            CmsPage myPage = new CmsPage(pageData1);
            myPage.Url = "~/newurl.cmsx";
            _dataHelper.UpdatePage(myPage);

            DataRow pageData2 = SqlHelper.ExecuteDataset(_connectionString,
                CommandType.Text, "SELECT * FROM [cms_Page] WHERE [Id] = 1").Tables[0].Rows[0];

            Assert.AreNotEqual(pageData1["Url"].ToString(), pageData2["Url"].ToString());
            Assert.AreEqual("~/newurl.cmsx", pageData2["Url"].ToString());
            Assert.AreEqual(pageData1["Title"].ToString(), pageData2["Title"].ToString());
            Assert.AreEqual(pageData1["Description"].ToString(), pageData2["Description"].ToString());
            Assert.AreEqual(pageData1["Keywords"].ToString(), pageData2["Keywords"].ToString());
            Assert.AreEqual(pageData1["ContentText"].ToString(), pageData2["ContentText"].ToString());
            Assert.AreEqual((bool)pageData1["Enabled"], (bool)pageData2["Enabled"]);
        }

    }
}
